package org.qf.web;

import org.qf.pojo.SmbmsUser;
import org.qf.service.SmbmsUserService;
import org.qf.service.impl.SmbmsUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Mei
 */
@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面的值
//        req.setCharacterEncoding("utf-8");
        String userCode=req.getParameter("userCode");

        String pwd=req.getParameter("password");

        //调用业务层代码
        SmbmsUserService smbmsUserService=new SmbmsUserServiceImpl();

        try {
            SmbmsUser login = smbmsUserService.login(userCode, pwd);
            if(null !=login){
                //获取session
                HttpSession session=req.getSession();
                //session一次会话 将一个会员对象保存至session域中
                session.setAttribute("userSession",login);
                //转发
                req.getRequestDispatcher("welcome.jsp").forward(req,resp);
            }else{
                resp.sendRedirect("login.jsp");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
